package com.cv.system.dal.mapper.logger;

import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.mybatis.core.mapper.BaseMapperX;
import com.cv.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cv.system.controller.system.logger.vo.loginLog.SystemLoginLogPageReqVO;
import com.cv.system.controller.system.logger.vo.loginLog.SystemLoginLogRespVO;
import com.cv.system.dal.dataobject.logger.SystemLoginLogDO;
import com.cv.system.dal.dataobject.user.SystemUsersDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 系统登录日志数据层
 *
 * @author Charles_XDXD
 */
@Mapper
public interface SystemLoginLogMapper extends BaseMapperX<SystemLoginLogDO> {

    default PageResult<SystemLoginLogRespVO> getSystemLoginLogPage(SystemLoginLogPageReqVO pageReqVO) {
        return selectJoinPage(pageReqVO, SystemLoginLogRespVO.class, new MPJLambdaWrapperX<SystemLoginLogDO>()
                .selectAll(SystemLoginLogDO.class)
                .likeIfPresent(SystemLoginLogDO::getUserIp, pageReqVO.getUserIp())
                .eqIfPresent(SystemLoginLogDO::getResult, pageReqVO.getResult())
                .eqIfPresent(SystemLoginLogDO::getLogType, pageReqVO.getLogType())
                .betweenIfPresent(SystemLoginLogDO::getCreateTime, pageReqVO.getCreateTime())
                .leftJoin(SystemUsersDO.class, SystemUsersDO::getId, SystemLoginLogDO::getUserId)
                .selectAs(SystemUsersDO::getUsername, SystemLoginLogRespVO::getUsername)
                .likeIfExists(SystemUsersDO::getUsername, pageReqVO.getUsername())
                .orderByDesc(SystemLoginLogDO::getCreateTime)
        );
    }

}
